/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package astroLib;

import java.util.Calendar;


/**
 *
 * @author mehmetrg
 */
public class SunMoonPosition {
    
    
     private Horizontal sunPosition, moonPosition;
     private double  MJD0, MJD,Hour,T ;
     private double moonPhase;
     private EarthHeading qiblaInfo;
     private Calendar now;
     private double timeZone;

       // private double timeZone;
     //private EarthPosition istanbul=new EarthPosition(28.95,41);
/* public SunMoonPosition() {
     this (new EarthPosition(Math.toRadians(41),Math.toRadians(28.75)),2008,3,15,20,41,0,2);
 }*/
     //public SunMoonPosition(EarthPosition earth, Calendar now, double timeZone)
/* public SunMoonPosition(EarthPosition earth, Calendar now) {

    double latitude=Math.toRadians(earth.getLatitude());
    double longitude=Math.toRadians(earth.getLongitude());
    now = Calendar.getInstance();
       if (Settings.locationPrefer.getTimeZoneSrc()!=0)
         {
            timeZone=Settings.locationPrefer.getTimeZone();
            if (Settings.optionPref.getIsDSTOn()) timeZone++;
        }
        else
            timeZone=now.getTimeZone().getRawOffset()/(60*60*1000);
   // double rad=Math.PI/180;
    //timeZone=now.getTimeZone().getRawOffset()/(60*60*1000);
    MJD0=APC_Time.Mjd(now.get(Calendar.YEAR), now.get(Calendar.MONTH)+1, now.get(Calendar.DAY_OF_MONTH),now.get(Calendar.HOUR_OF_DAY),now.get(Calendar.MINUTE),now.get(Calendar.MINUTE))- timeZone/24.0;
    //MJD0=APC_Time.Mjd(year,month,day,hour,minute,second) - timeZone/24.0;     
    MJD = MJD0 + Hour/24.0;   //545393368055555555      
    T   = (MJD-51544.5)/36525.0;
    //APC_Sun sunData= new APC_Sun();
    //APC_Moon moonData= new APC_Moon();
    moonPosition=APC_Moon.MiniMoon(T).Equ2Hor(latitude,longitude,MJD);//az=183.5858
    sunPosition= APC_Sun.MiniSun(T).Equ2Hor(latitude,longitude,MJD);
    //System.out.println(moonPosition.Az);
    //System.out.println(moonPosition.h);
    
    double E= APC_Sun.L-APC_Moon.l_Moon;//l_moon 1.4421 L=6.18064// E=4.73850629772695878
    //EarthPosition  qibla = new EarthPosition(21.416666667, 39.816666);
   // qiblaInfo=earth.toEarthHeading(qibla);
    // moonPhase = (1 + cos(pi - E)) / 2;
    moonPhase=(1+Math.cos(Math.PI-E))/2;//48694254279852139 e-17
    //System.out.println(qiblaInfo.getKiloMetres());
}*/


  public SunMoonPosition(EarthPosition earth) {

    double latitude=Math.toRadians(earth.getLatitude());
    double longitude=Math.toRadians(earth.getLongitude());
    now = Calendar.getInstance();
       if (Settings.locationPrefer.getTimeZoneSrc()!=0)
         {
            timeZone=Settings.locationPrefer.getTimeZone();
            if (Settings.locationPrefer.getIsDSTOn()) timeZone++;
        }
        else
            timeZone=now.getTimeZone().getRawOffset()/(60*60*1000);
   // double rad=Math.PI/180;
    //timeZone=now.getTimeZone().getRawOffset()/(60*60*1000);
    MJD0=APC_Time.Mjd(now.get(Calendar.YEAR), now.get(Calendar.MONTH)+1, now.get(Calendar.DAY_OF_MONTH),now.get(Calendar.HOUR_OF_DAY),now.get(Calendar.MINUTE),now.get(Calendar.MINUTE))- timeZone/24.0;
    //MJD0=APC_Time.Mjd(year,month,day,hour,minute,second) - timeZone/24.0;
    MJD = MJD0 + Hour/24.0;   //545393368055555555
    T   = (MJD-51544.5)/36525.0;
    //APC_Sun sunData= new APC_Sun();
    //APC_Moon moonData= new APC_Moon();
    moonPosition=APC_Moon.MiniMoon(T).Equ2Hor(latitude,longitude,MJD);//az=183.5858
    sunPosition= APC_Sun.MiniSun(T).Equ2Hor(latitude,longitude,MJD);
    //System.out.println(moonPosition.Az);
    //System.out.println(moonPosition.h);

    double E= APC_Sun.L-APC_Moon.l_Moon;//l_moon 1.4421 L=6.18064// E=4.73850629772695878
    EarthPosition  qibla = new EarthPosition(21.416666667, 39.816666);
    qiblaInfo=earth.toEarthHeading(qibla);
        // moonPhase = (1 + cos(pi - E)) / 2;
    moonPhase=(1+Math.cos(Math.PI-E))/2;//48694254279852139 e-17
    //System.out.println(qiblaInfo.getKiloMetres());
}

 
    public Horizontal getSunPosition() {
        return sunPosition;
    }
    
    public Horizontal getMoonPosition() {
        return moonPosition;
    }
    
    public double getMoonPhase() {
        return moonPhase;
    }
    
   public EarthHeading getDestinationHeading() {
        return qiblaInfo;
    }

}